package com.tools.starter.swagger.sqlite;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;

/**
 * @author com.tools
 * @version 1.0
 * @Description: SQLiteManager
 * <com.tools.starter.swagger.sqlite>
 * @date Created in 2020/8/27 18:05
 **/
@Slf4j
public class SQLiteManager {

    private static Connection c;

    public static void setConnection(Connection connection) {
        c = connection;
    }

    public static int save(MockEntity entity){
        Statement stmt = getStatement();
        // fixme 后续添加事物提交
        try {
            return save(entity, stmt);
        } catch (SQLException e) {
            log.error("save mock error, param:{}", entity);
        }finally {
            closeStatement(stmt);
            return 0;
        }
    }

    public static int save(MockEntity entity, Statement stmt) throws Exception{
        MockEntity item = selectByPathUrl(entity.getPathUrl());
        String sql = null;
        if (Objects.isNull(item)) {
            sql = "INSERT INTO MOCK (tags,summary,path_url,mock_data) VALUES ('" + entity.getTags() + "', '" + entity.getSummary() + "', '" + entity.getPathUrl()+ "', '" + (StringUtils.isEmpty(entity.getMockData()) ? "" : entity.getMockData()) + "');";
        } else {
            sql = "update MOCK set id = id";
            if (StringUtils.hasLength(entity.getTags())) {
                sql += ", tags = '" + entity.getTags() +"'";
            }
            if (StringUtils.hasLength(entity.getSummary())) {
                sql += ", summary = '" + entity.getSummary() + "'";
            }
            if (StringUtils.hasLength(entity.getMockData())) {
                sql += ", mock_data = '" + entity.getMockData() + "'";
            }
            sql += " where path_url = '" + entity.getPathUrl()+ "';";
        }
        return stmt.executeUpdate(sql);
    }

    public static Statement getStatement() {
        try {
            Statement stmt = c.createStatement();
            return stmt;
        } catch (SQLException e) {
            log.error("get statement error", e);
            throw new RuntimeException(e);
        }
    }

    public static void closeStatement(Statement stmt){
        if (Objects.nonNull(stmt)) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.error("close statement error", e);
            }
        }
    }

    public static MockEntity selectByPathUrl(String pathUrl) {
        MockEntity item = null;
        try {
            Statement stmt = getStatement();
            String sql = "select id, tags, summary, path_url, mock_data from MOCK where path_url = '"+ pathUrl + "';";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                item = new MockEntity();
                item.setId(rs.getInt("id"));
                item.setPathUrl(rs.getString("path_url"));
                item.setSummary(rs.getString("summary"));
                item.setTags(rs.getString("tags"));
                item.setMockData(rs.getString("mock_data"));
            }
            rs.close();
            closeStatement(stmt);
        } catch (SQLException e) {
            log.error("path_url:{} mock data error", pathUrl);
        }
        return item;
    }
}
